using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class SpecialEventsController : Controller
    {
        public SpecialEventsController(IRepository<SpecialEvent> specialEventRepository) {
            Check.Require(specialEventRepository != null, "specialEventRepository may not be null");

            this.specialEventRepository = specialEventRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<SpecialEvent> specialEvents = specialEventRepository.GetAll();
            return View(specialEvents);
        }

        [Transaction]
        public ActionResult Show(int id) {
            SpecialEvent specialEvent = specialEventRepository.Get(id);
            return View(specialEvent);
        }

        public ActionResult Create() {
            SpecialEventFormViewModel viewModel = SpecialEventFormViewModel.CreateSpecialEventFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(SpecialEvent specialEvent) {
            if (ViewData.ModelState.IsValid && specialEvent.IsValid()) {
                specialEventRepository.SaveOrUpdate(specialEvent);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The specialEvent was successfully created.";
                return RedirectToAction("Index");
            }

            SpecialEventFormViewModel viewModel = SpecialEventFormViewModel.CreateSpecialEventFormViewModel();
            viewModel.SpecialEvent = specialEvent;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            SpecialEventFormViewModel viewModel = SpecialEventFormViewModel.CreateSpecialEventFormViewModel();
            viewModel.SpecialEvent = specialEventRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(SpecialEvent specialEvent) {
            SpecialEvent specialEventToUpdate = specialEventRepository.Get(specialEvent.Id);
            TransferFormValuesTo(specialEventToUpdate, specialEvent);

            if (ViewData.ModelState.IsValid && specialEvent.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The specialEvent was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                specialEventRepository.DbContext.RollbackTransaction();

				SpecialEventFormViewModel viewModel = SpecialEventFormViewModel.CreateSpecialEventFormViewModel();
				viewModel.SpecialEvent = specialEvent;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(SpecialEvent specialEventToUpdate, SpecialEvent specialEventFromForm) {
			specialEventToUpdate.Name = specialEventFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The specialEvent was successfully deleted.";
            SpecialEvent specialEventToDelete = specialEventRepository.Get(id);

            if (specialEventToDelete != null) {
                specialEventRepository.Delete(specialEventToDelete);

                try {
                    specialEventRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the specialEvent from being deleted. " +
						"Another item likely depends on this specialEvent.";
                    specialEventRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The specialEvent could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the SpecialEvent form for creates and edits
		/// </summary>
        public class SpecialEventFormViewModel
        {
            private SpecialEventFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static SpecialEventFormViewModel CreateSpecialEventFormViewModel() {
                SpecialEventFormViewModel viewModel = new SpecialEventFormViewModel();
                
                return viewModel;
            }

            public SpecialEvent SpecialEvent { get; internal set; }
        }

        private readonly IRepository<SpecialEvent> specialEventRepository;
    }
}
